package com.charley.icloud.admin.webapps.sysmenu.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.charley.icloud.admin.common.constant.SysTableConstants;
import com.charley.icloud.admin.webapps.sysmenu.pojo.entity.SysMenuDO;
import com.charley.icloud.admin.webapps.sysmenu.pojo.request.SysMenuQuery;
import com.charley.icloud.admin.webapps.sysmenu.service.SysMenuService;
import com.charley.icloud.common.constants.SystemNumberConstants;
import com.charley.icloud.common.response.result.HttpResponseResult;
import com.charley.icloud.common.response.result.HttpResponseResultFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/menus")
@Tag(name = "菜单控制器")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @PostMapping(value = "/save")
    @Operation(summary = "保存菜单", description = "后台管理系统")
    public HttpResponseResult save(@RequestBody SysMenuQuery query) {
        if (query != null) {
            SysMenuDO record = new SysMenuDO();
            BeanUtil.copyProperties(query, record, CopyOptions.create().ignoreCase().ignoreNullValue());
            return HttpResponseResultFactory.success(sysMenuService.save(record));
        }
        return HttpResponseResultFactory.fail("参数异常，请重试...");
    }

    @PostMapping(value = "/delete")
    @Operation(summary = "删除菜单", description = "后台管理系统")
    public HttpResponseResult delete(@RequestBody List<SysMenuQuery> queries) {
        List<SysMenuDO> records = new ArrayList<>();
        if (queries != null && queries.size() > SystemNumberConstants.SYSTEM_NUMBER_INTEGER_ZERO) {
            for (SysMenuQuery query : queries) {
                SysMenuDO sysMenuDO = new SysMenuDO();
                BeanUtil.copyProperties(query, sysMenuDO, CopyOptions.create().ignoreCase().ignoreNullValue());
                records.add(sysMenuDO);
            }
        }
        return HttpResponseResultFactory.success(sysMenuService.delete(records));
    }

    @GetMapping(value = "/findNavTree")
    @Operation(summary = "查询导航菜单树", description = "后台管理系统")
    @Parameter(name = "userId", description = "用户ID", required = true, in = ParameterIn.PATH)
    public HttpResponseResult findNavTree(@RequestParam String userId) {
        return HttpResponseResultFactory.success(sysMenuService.findTree(userId, SysTableConstants.SYS_MENU_TABLE_TYPE_OPT_MENU));
    }

    @GetMapping(value = "/findMenuTree")
    @Operation(summary = "查询菜单树", description = "后台管理系统")
    public HttpResponseResult findMenuTree() {
        return HttpResponseResultFactory.success(sysMenuService.findTree(null, SysTableConstants.SYS_MENU_TABLE_TYPE_OPT_MENU_BUTTON));
    }
}
